﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
using AISservice.Common;
using AISservice.Entity;
using Common;
using SqlSugar;

namespace AISservice.BLL
{
    public class orderHandler
    {
        public DataTableCollection GetOrder(string ordernumber)
        {
            var sql = $"SELECT * FROM dbo.fn_getOrderDetailByordernumber('{ordernumber}')";
            sql = $"exec  sp_getOrderDetailByordernumber  '{ordernumber}'";
            Log4Net.Debug($"{sql}");
            SomeHandler.LogToken(ordernumber);
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            return data;
            
        }
        public DataTableCollection GetOrderFrompickinggoods(string ordernumber)
        {
            var sql = $"SELECT * FROM dbo.fn_getOrderDetailBypickinggoods('{ordernumber}')";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            Log4Net.Debug($"{sql}");
            return data;
        }

        public DataTableCollection GetNTOrder(string ordernumber)
        {
            var sql = $"SELECT * FROM dbo.fn_getOrderDetailByordernumberForNT('{ordernumber}')";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            return data;

        }


        public DataTableCollection GetTransferOrder(string orderid)
        {
            var sql = $" exec tp_bltransferdetail_CreateITOrder  '{orderid}' ";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            Log4Net.Debug($"{sql}");
            return data;

        }

        public DataTableCollection GetTransferOrderForF5(string orderid)
        {
            var sql = $" exec tp_bltransferdetail_CreateF5Order  '{orderid}' ";
            Log4Net.Debug($"{sql}");
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            return data;

        }


        

        public DataTableCollection GetJEOrder(string orderid)
        {
            var sql = $" exec tp_bljournal_CreateJEOrder  '{orderid}' ";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            Log4Net.Debug($"{sql}");
            SomeHandler.LogToken(orderid);
            return data;

        }


        public DataTableCollection GetNWOrder(string orderid)
        {
            var sql = $"SELECT *,'Z' as LOB FROM  v_blpickinggoodsandDetail WHERE sys_id ='{orderid}' ";

            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            Log4Net.Debug($"{sql}");
            return data;
        }

        public DataTableCollection GetNROrder(string orderid)
        {
            var sql = $"SELECT *  FROM  v_blPurchaseOrderandDetail WHERE sys_id ='{orderid}' AND sys_status=150";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            return data;
        }


        public DataTableCollection GetReturnNROrder(string orderid)
        {
            var sql = $"SELECT *  FROM  v_blPurchaseReturnandDetail WHERE sys_id ='{orderid}' ";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            return data;
        }

        public DataTableCollection GetNVOrder(string orderid)
        {
            var sql = $"EXEC tp_blInvoiceDeduction_CreateNV '{orderid}' ";
            var data = SqlHelper.GetTable(CommandType.Text, sql, new SqlParameter[] { });
            return data;
        }



        public List<v_blStockadjust> GetF6Order(string orderid)
        {
            //return SqlHelper.sugarclient.Queryable<blTransfer, blTransferDetail>((i, s) => new object[]
            //                          {
            //                             JoinType.Left,i.sys_id==s.sys_masterid
            //                          })
            //                         .Where(i => i.sys_id == Guid.Parse(orderid))
            //                         .Select<v_blTransfer>().ToList();

            return   SqlHelper.sugarclient.Queryable<blStockadjust,blStockadjustDetail>(
               (t,td)=>new JoinQueryInfos(JoinType.Left,t.sys_id==td.sys_masterid)
               )
               .Where(t => t.sys_id == Guid.Parse(orderid))
               .Select<v_blStockadjust>().ToList();

 


        }





    }
}
